package com.macaku.util;

import com.macaku.common.SystemJsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.Scanner;

@Slf4j
public class RedissonBloomFilter {
    private static final RBloomFilter<String> BLOOM_FILTER;

    private static final String REDIS_ADDRESS = "redis://127.0.0.1:6379";

    private static final long NUM_BITS = (long) 1e8;

    private static final String BLOOM_FILTER_NAME = "urlList";

    private static final double RATE = 0.03;//不存在误判为存在的概率


    static {
        Config config = new Config();
        config.useSingleServer().setAddress(REDIS_ADDRESS);//没有密码就不用设置
        //构造Redisson
        RedissonClient redisson = Redisson.create(config);
        BLOOM_FILTER = redisson.getBloomFilter(BLOOM_FILTER_NAME);
        BLOOM_FILTER.tryInit(NUM_BITS, RATE);
    }

    public static boolean add(String key) {
        log.info(key + "  加入布隆过滤器");
        return BLOOM_FILTER.add(key);
    }
    public static boolean contains(String key) {
        boolean flag = BLOOM_FILTER.contains(key);
        log.info(key +  "是否在布隆过滤器：" + flag);
        return flag;
    }
 }